package cn.elricyu.tt.action.chat;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import net.sf.json.JSON;
import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.elricyu.tt.action.base.AbstractAction;
import cn.elricyu.tt.action.city.CityMainAction;
import cn.elricyu.tt.bean.ChatMessage;
import cn.elricyu.tt.bean.User;
import cn.elricyu.tt.pattern.MessageSubject;
import cn.elricyu.tt.store.SystemContainer;
import cn.elricyu.tt.util.JsonUtil;

public class ChatAction extends AbstractAction<List<ChatMessage>> {
	private static Log logger = LogFactory.getLog(CityMainAction.class);
	private User self = null;

	public ChatAction(User self) {
		this.self = self;
		this.url = "root/chat.action";
		this.showTips = false;
		this.toPause = false;
	}

	public void process(JSON json) {
	}

	public static void doParseAll(String key, JSON json) {
		try {
			JSONObject chats = JsonUtil.getJsonObject(json, "chats");
			if (chats == null) {
				return;
			}
			List<ChatMessage> messageList = new ArrayList<ChatMessage>();
			for (Channel channel : Channel.values()) {
				List<JSONObject> chatlogList = JsonUtil.getListJsonValue(chats,
						channel.getJsonKey());
				List<ChatMessage> subList = doParseList(key, chatlogList,
						channel);
				if (!subList.isEmpty()) {
					messageList.addAll(subList);
				}
			}
			if (!messageList.isEmpty()) {
				Collections.sort(messageList);
				MessageSubject.notifyObservers(key, messageList);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static List<ChatMessage> doParseList(String key,
			List<JSONObject> chatlogList, Channel channel) {

		if (chatlogList.isEmpty()) {
			return Collections.emptyList();
		}
		List<ChatMessage> messageList = new ArrayList<ChatMessage>();
		if (chatlogList.size() == 1) {
			JSONObject chatlog = JsonUtil.getJsonObject(chatlogList.get(0),
					"chatlog");
			ChatMessage message = doParse(chatlog, channel);
			boolean add = SystemContainer.storeMessage(key, message);
			if (add) {
				messageList.add(message);
			}
		} else {
			for (JSONObject jsonObject : chatlogList) {
				ChatMessage message = doParse(jsonObject, channel);
				boolean add = SystemContainer.storeMessage(key, message);
				if (add) {
					messageList.add(message);
				}
			}
		}
		return messageList;
	}

	private static ChatMessage doParse(JSONObject chatlog, Channel channel) {
		String playerid = JsonUtil.getStringValue(chatlog, "playerid", "");
		String name = JsonUtil.getStringValue(chatlog, "name", "");
		String content = JsonUtil.getStringValue(chatlog, "content", "");
		long time = JsonUtil.getLongValue(chatlog, "time", 0l);
		ChatMessage message = new ChatMessage();
		message.setTime(time);
		message.setChannel(channel.getChannel());
		message.setPlayerid(playerid);
		message.setName(name);
		message.setContent(content);
		return message;
	}

}
